package com.usian.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.usian.common.constant.user.UserConstants;
import com.usian.model.article.pojo.ApAuthor;
import com.usian.model.common.dtos.PageResponseResult;
import com.usian.model.common.dtos.ResponseResult;
import com.usian.model.common.enums.AppHttpCodeEnum;
import com.usian.model.media.pojos.WmUser;
import com.usian.model.user.dtos.AuthDto;
import com.usian.model.user.pojos.ApUser;
import com.usian.model.user.pojos.ApUserRealname;
import com.usian.user.feign.ArticleFeign;
import com.usian.user.feign.WemediaFeign;
import com.usian.user.mapper.ApUserMapper;
import com.usian.user.mapper.AuthMapper;
import com.usian.user.service.AuthService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;

@Service
public class AuthServiceImpl extends ServiceImpl<AuthMapper, ApUserRealname> implements AuthService {
    @Resource
    private ApUserMapper apUserMapper;
    @Autowired
    private WemediaFeign wemediaFeign;
    @Autowired
    private ArticleFeign articleFeign;

    @Override
    public ResponseResult loadListByStatus(AuthDto authDto) {
        //1.检查参数
        if(authDto==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //2.检查分页参数
        authDto.checkParam();
        //3.创建条件
        LambdaQueryWrapper<ApUserRealname> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(authDto.getStatus()!=null,ApUserRealname::getStatus,authDto.getStatus());
        //4.进行查询
        Page pageInfo=new Page(authDto.getPage(),authDto.getSize());
        //分页
        IPage page = page(pageInfo,lambdaQueryWrapper);
        //封装参数
        ResponseResult result = new PageResponseResult(authDto.getPage(), authDto.getSize(), (int) page.getTotal());
        //取出分页数据，存放在result对象中
        result.setData(page.getRecords());
        return result;
    }

    @Transactional
    @Override
    public ResponseResult updateStatusById(AuthDto authDto, Integer status) {
        //1.检查参数
        if(authDto==null||authDto.getId()==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //检查状态
        if(checkStatus(status)){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //2.修改ap_user_realname状态
        //2.1创建ApUserRealname对象  用来保存需要修改中传递的参数
        ApUserRealname apUserRealname = new ApUserRealname();
        //用户的id和状态设置进来
        apUserRealname.setId(authDto.getId());
        apUserRealname.setStatus(status);
        //判定dto中的审核结果信息
        if(authDto.getMsg()!=null){
            apUserRealname.setReason(authDto.getMsg());
        }
        //2.3更新
        updateById(apUserRealname);
        //3.添加自媒体人的账号和作家号
        if(status.equals(UserConstants.PASS_AUTH)){
            ResponseResult result = createWemediaAndAuthor(authDto);
            if(result!=null){
                return result;
            }
        }
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
    //添加自媒体账号和作家号
    public ResponseResult createWemediaAndAuthor(AuthDto dto){
        //3.添加自媒体人的账号
        //3.1查询ApUserRealname信息  根据dto中提供的信息来进行查询
        ApUserRealname apUserRealname = getById(dto.getId());
        //3.2根据userid 查询ApUser的相关数据
        ApUser apUser = apUserMapper.selectById(apUserRealname.getUserId());
        if(apUser==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //3.3根据ApUser中的用户名查询自媒体账号是否存在
        WmUser wmUser = wemediaFeign.findByName(apUser.getName());
        if(wmUser==null){
            //3.4添加自媒体账号
            wmUser = new WmUser();
            wmUser.setApUserId(apUser.getId());
            wmUser.setCreatedTime(new Date());
            wmUser.setSalt(apUser.getSalt());
            wmUser.setName(apUser.getName());
            wmUser.setPassword(apUser.getPassword());
            wmUser.setStatus(9);
            wmUser.setPhone(apUser.getPhone());
            wemediaFeign.save(wmUser);
        }
        //4.添加作者
        createAuthor(wmUser);
        return null;
    }
    //添加作家号
    public void createAuthor(WmUser wmUser){
        //1.根据userid查询当前用户是否有作者号
        ApAuthor apAuthor = articleFeign.findByUserId(wmUser.getApUserId());
        if(apAuthor==null){
            //创建作家号
           apAuthor = new ApAuthor();
           apAuthor.setName(wmUser.getName());
           apAuthor.setType(UserConstants.AUTHOR_TYPE);
           apAuthor.setCreatedTime(new Date());
           apAuthor.setUserId(wmUser.getApUserId());
           articleFeign.save(apAuthor);

        }
    }

    public boolean checkStatus(Integer status){
        if(status!=null){
            return false;
        }
        return true;
    }
}
